#coding=utf-8
import arcpy
import json

arcpy.env.overwriteOutput = True
spatial_ref = arcpy.SpatialReference(4326)
def createSHPFromJSON(filename):
    with open((r'D:\WorkData\20250712四川河流\成都\%s.json'%filename).decode('utf-8'), 'rb') as file:
        content = file.read()
    data = json.loads(content)

    dataCount = len(data['features'])
    ArcpyPolyline_List = []
    for i in range(dataCount):
        coordsList_Top = data['features'][i]['geometry']
        name = data['features'][i]['name']
        rivId = int(data['features'][i]['osid'])
        totalLen = "%s"%data['features'][i]['rlen']
        curLen = "%s"%data['features'][i]['curlen']
        area = "%s"%data['features'][i]['area']
        flow = "%s"%data['features'][i]['flow']
        PCDM = "%s"%data['features'][i]['censcode']
        sPCounty = "%s"%data['features'][i]['start_place']
        ePCounty = "%s"%data['features'][i]['end_place']
        level = "%s"%data['features'][i]['branchlevel']
        croType = "%s"%data['features'][i]['crosstype']
        print (i)
        for coordsList in coordsList_Top:
            ArcpyPoint_List = [arcpy.Point(*coords) for coords in coordsList]
            ArcpyArray_List = arcpy.Array(ArcpyPoint_List)
            ArcpyPolyline = arcpy.Polyline(ArcpyArray_List, spatial_ref)
            ArcpyPolyline_List.append([ArcpyPolyline, name,rivId, totalLen, curLen,area,flow,PCDM,sPCounty,ePCounty,level,croType])
    c_polyline = arcpy.CreateFeatureclass_management(r'D:\WorkData\20250712四川河流\shp', '%s.shp' % filename.split('-')[0], 'POLYLINE',spatial_reference=spatial_ref)
    arcpy.AddField_management(c_polyline, 'name', 'TEXT')
    arcpy.AddField_management(c_polyline, 'rivId', 'TEXT')
    arcpy.AddField_management(c_polyline, 'totalLen', 'TEXT')
    arcpy.AddField_management(c_polyline, 'curLen', 'TEXT')
    arcpy.AddField_management(c_polyline, 'area', 'TEXT')
    arcpy.AddField_management(c_polyline, 'flow', 'TEXT')
    arcpy.AddField_management(c_polyline, 'PCDM', 'TEXT')
    arcpy.AddField_management(c_polyline, 'sPCounty', 'TEXT')
    arcpy.AddField_management(c_polyline, 'ePCounty', 'TEXT')
    arcpy.AddField_management(c_polyline, 'level', 'TEXT')
    arcpy.AddField_management(c_polyline, 'croType', 'TEXT')
    cs = arcpy.da.InsertCursor(c_polyline, ['SHAPE@', 'name', 'rivId','totalLen', 'curLen','area','flow','PCDM','sPCounty','ePCounty','level','croType'])
    for ArcpyPolyline in ArcpyPolyline_List:
        cs.insertRow(ArcpyPolyline)
    del cs

createSHPFromJSON('甘孜-0-885')